package com.sykj.bsc.jhy.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.sykj.bsc.jhy.entity.Bis_nurse_project;
import com.sykj.bsc.jhy.entity.Bis_nurse_type;
import com.sykj.bsc.jhy.service.Bis_nurse_projectService;




@Controller
@RequestMapping("jhy")
public class Bis_nurse_projectController {
	
	@Autowired Bis_nurse_projectService impl;
	
	
	//显示护理项目列表
	@RequestMapping("nurseProject")
	public String selectBis_nurse_project(Bis_nurse_project bis_nurse_project,Model model) {

		List<Bis_nurse_project> list = impl.selectBis_nurse_project(bis_nurse_project);
		model.addAttribute("list", list);	
		model.addAttribute("tlist", impl.selectBis_nurse_type());
		return "backstage/jhy/nurseProject";
	}
	
	//删除护理项目列表记录
	@RequestMapping("del/{project_id}")
	public String delBis_nurse_project(@PathVariable("project_id") Integer project_id) {
		impl.delBis_nurse_project(project_id);
		return "redirect:/jhy/nurseProject";
	}
	
	//新增护理项目
	@RequestMapping("add")
	public String selectBis_nurse_type(Bis_nurse_type bis_nurse_type,Model model) {
		model.addAttribute("tlist", impl.selectBis_nurse_type());
		return "backstage/jhy/addBis_nurse_project";
	}
	
	@RequestMapping("addBis_nurse_project")
	public String addBis_nurse_project(Bis_nurse_project bis_nurse_project,HttpServletRequest request,
			@RequestParam(value="file1")MultipartFile pictureFile1,@RequestParam(value="file2")MultipartFile pictureFile2) {
		 //这个RequestParam(value="file")的是我们在jsp的name=“file”
		// 使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取文件的扩展名
		String ext1 = FilenameUtils.getExtension(pictureFile1
				.getOriginalFilename());
		String ext2 = FilenameUtils.getExtension(pictureFile2
				.getOriginalFilename());
		// 设置图片上传路径
		String url = request.getSession().getServletContext()
				.getRealPath("/js/upload");
		//System.out.println(url);
		// 以绝对路径保存重名命后的图片
		try {
			pictureFile1.transferTo(new File(url + "/" + name + "." + ext1));
			pictureFile2.transferTo(new File(url + "/" + name + "." + ext2));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 把图片存储路径保存到数据库
		bis_nurse_project.setCover_big_photo("/upload/" + name + "." + ext1);
		bis_nurse_project.setCover_photo("/upload/" + name + "." + ext2);
		
		impl.addBis_nurse_project(bis_nurse_project);
		return "redirect:/jhy/nurseProject";
	}
	
	//查看详情
	@RequestMapping("selectById/{project_id}")
	public String selectById(@PathVariable("project_id") Integer project_id,Model model) {
		Bis_nurse_project bnp = impl.queryById(project_id);
		model.addAttribute("bnp", bnp);
		model.addAttribute("tlist", impl.selectBis_nurse_type());
		return "backstage/jhy/seeBis_nurse_project";
	}
	
	//修改
	@RequestMapping("queryById/{project_id}")
	public String queryById(@PathVariable("project_id") Integer project_id,Model model) {
		Bis_nurse_project bnp = impl.queryById(project_id);
		model.addAttribute("bnp", bnp);
		model.addAttribute("tlist", impl.selectBis_nurse_type());
		return "backstage/jhy/updateBis_nurse_project";
	}
	
	@RequestMapping("updateBis_nurse_project")
	public String updateBis_nurse_project(Bis_nurse_project bis_nurse_project,HttpServletRequest request,
			@RequestParam(value="file1")MultipartFile pictureFile1,@RequestParam(value="file2")MultipartFile pictureFile2) {
		 //这个RequestParam(value="file")的是我们在jsp的name=“file”
			// 使用UUID给图片重命名，并去掉四个“-”
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件的扩展名
			String ext1 = FilenameUtils.getExtension(pictureFile1
					.getOriginalFilename());
			String ext2 = FilenameUtils.getExtension(pictureFile2
					.getOriginalFilename());
			// 设置图片上传路径
			String url = request.getSession().getServletContext()
					.getRealPath("/js/upload");
			//System.out.println(url);
			// 以绝对路径保存重名命后的图片
			try {
				pictureFile1.transferTo(new File(url + "/" + name + "." + ext1));
				pictureFile2.transferTo(new File(url + "/" + name + "." + ext2));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// 把图片存储路径保存到数据库
			bis_nurse_project.setCover_big_photo("upload/" + name + "." + ext1);
			bis_nurse_project.setCover_photo("upload/" + name + "." + ext2);
		System.out.println(bis_nurse_project);
		impl.updateBis_nurse_project(bis_nurse_project);
		return "redirect:/jhy/nurseProject";
	}
	
	
	/*//前台护理列表
	@RequestMapping("Bis_nurse_projectReception")
	public String Bis_nurse_projectReception(Bis_nurse_project bis_nurse_project,Model model) {

		List<Bis_nurse_project> list = impl.selectBis_nurse_projectReception(bis_nurse_project);
		model.addAttribute("list", list);	
		model.addAttribute("tlist", impl.selectBis_nurse_type());

		return "foreground/jhy/Bis_nurse_projectReception";

	}
=======
>>>>>>> branch 'master' of https://gitee.com/http_a1/bsc.git
	
<<<<<<< HEAD
	//前台护理详情
	
	@RequestMapping("selectByIdproject/{project_id}")
	public String selectByIdproject(@PathVariable("project_id") Integer project_id,Model model) {
		Bis_nurse_project bnp = impl.queryById(project_id);
		model.addAttribute("bnp", bnp);

		return "foreground/jhy/qiantaiproject";

	}
	*/
}
